What is @smithy/fetch-http-handler?
@smithy/fetch-http-handler is a package that provides an HTTP handler for making requests using the Fetch API. It is part of the AWS SDK for JavaScript v3 and is designed to work seamlessly with other Smithy components to handle HTTP requests and responses.
What are @smithy/fetch-http-handler's main functionalities?
Basic HTTP Request
This feature allows you to make a basic HTTP GET request using the FetchHttpHandler. The request object specifies the method, hostname, and path for the request.
const { FetchHttpHandler } = require('@smithy/fetch-http-handler');
const handler = new FetchHttpHandler();
const request = {
method: 'GET',
hostname: 'example.com',
path: '/path'
};
handler.handle(request).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
Custom HTTP Headers
This feature allows you to add custom HTTP headers to your request. The headers object within the request object specifies the custom headers.
const { FetchHttpHandler } = require('@smithy/fetch-http-handler');
const handler = new FetchHttpHandler();
const request = {
method: 'GET',
hostname: 'example.com',
path: '/path',
headers: {
'Custom-Header': 'value'
}
};
handler.handle(request).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
Handling HTTP Responses
This feature demonstrates how to handle HTTP responses. The response object contains the body, which can be processed further, such as converting it to text.
const { FetchHttpHandler } = require('@smithy/fetch-http-handler');
const handler = new FetchHttpHandler();
const request = {
method: 'GET',
hostname: 'example.com',
path: '/path'
};
handler.handle(request).then(response => {
return response.body.text();
}).then(body => {
console.log(body);
}).catch(error => {
console.error(error);
});
Other packages similar to @smithy/fetch-http-handler
axios
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and handling responses. Compared to @smithy/fetch-http-handler, Axios offers more features out of the box, such as request and response interceptors, automatic JSON data transformation, and support for older browsers.
node-fetch
Node-fetch is a lightweight module that brings the Fetch API to Node.js. It is similar to @smithy/fetch-http-handler in that it uses the Fetch API for making HTTP requests. However, node-fetch is more focused on providing a minimal implementation of the Fetch API for Node.js environments, whereas @smithy/fetch-http-handler is designed to integrate with the AWS SDK for JavaScript v3.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It supports promises, streams, retries, and many other features. Compared to @smithy/fetch-http-handler, Got offers a more extensive set of features and a more user-friendly API for making HTTP requests and handling responses.
@smithy/fetch-http-handler
This is the default requestHandler
used for browser applications.
Since Node.js introduced experimental Web Streams API in v16.5.0 and made it stable in v21.0.0,
you can consider using fetch-http-handler
in Node.js, although it's not recommended.
For the Node.js default requestHandler
implementation, see instead
@smithy/node-http-handler
.